package vis.app;

import java.util.Observable;

public class AppController extends Observable {
	public static final String CTRL_INIT = "INIT_DISPLAY";
	public static final String CTRL_RUN = "RUN_DISPLAY";
	public static final String CTRL_PAUSE = "PAUSE_DISPLAY";
	public static final String CTRL_RESUME = "RESUME_DISPLAY";
	public static final String CTRL_STOP = "STOP_DISPLAY";
	
	private static AppController _instance = null;

	private String currentVisualItem = "";

	public static AppController getInstance() {
		if (null == _instance) {
			_instance = new AppController();
		}
		return _instance;
	}

	private EAppState state = EAppState.STATE_NONE;

	public EAppState getAppState() {
		return state;
	}

	public boolean isReady() {
		return EAppState.STATE_READY == state;
	}

	public boolean isPaused() {
		return EAppState.STATE_PAUSED == state;
	}

	public boolean isRunning() {
		return EAppState.STATE_RUNNING == state;
	}

	public boolean isFinished() {
		return EAppState.STATE_FINISHED == state;
	}

	public void toggleRunningPaused() {
		EventControl eventCtr = new EventControl();

		if (isReady()) {
			eventCtr.setType(CTRL_RUN);
			state = EAppState.STATE_RUNNING;
		} else if (isRunning()) {
			eventCtr.setType(CTRL_PAUSE);
			state = EAppState.STATE_PAUSED;
		} else if (isPaused()) {
			eventCtr.setType(CTRL_RESUME);
			state = EAppState.STATE_RUNNING;
		}

		eventCtr.setData(currentVisualItem);
		setChanged();
		notifyObservers(eventCtr);
	}

	public void setStateFinished() {
		EventControl eventCtr = new EventControl();

		if (isRunning() || isPaused()) {
			state = EAppState.STATE_FINISHED;
			eventCtr.setType(CTRL_STOP);
			System.out.println("SYSTEM IS NOW TERMINED");
		}

		eventCtr.setData(currentVisualItem);
		setChanged();
		notifyObservers(eventCtr);
	}

	public void setStateReady() {
		state = EAppState.STATE_READY;
		System.out.println("SYSTEM IS NOW READY");
	}

	public void executeSketch(String sketchClassName) {
		this.currentVisualItem = sketchClassName;
		System.out.println("Current Sketch: " + sketchClassName);

		EventControl eventCtr = new EventControl();
		eventCtr.setType(CTRL_INIT);
		eventCtr.setData(currentVisualItem);
		setChanged();
		notifyObservers(eventCtr);
	}
}